// LuoSnapShotImport.h
/* */
#ifdef _DEBUG
#define SNAP_SHOT_DLL	L"LuoSnapShot_Debug.dll"
#else
#define SNAP_SHOT_DLL	L"LuoSnapShot.dll"
#endif

namespace LuoSnapShot {

	// DLLibLoader
	class DLLibLoader {
		HMODULE m_hDLL;

		typedef HRESULT (STDCALL *Activate_t) (LPCWSTR token, void **obj);

		Activate_t m_func;
	public:
		DLLibLoader() :
		  m_hDLL(0),
			  m_func(0)
		  {
		  }

		  ~DLLibLoader()
		  {
			  if (m_hDLL != 0) {
				  FreeLibrary(m_hDLL);
			  }
		  }

		  HRESULT Activate(LPCWSTR token, void ** obj)
		  {
			  if (m_hDLL == 0) {
				  m_hDLL = LoadLibraryW(SNAP_SHOT_DLL);

				  if (m_hDLL == 0) {
					  DWORD error = GetLastError();

					  WCHAR errorInfo[256];
					  swprintf_s(errorInfo, 256, L"ERROR: Missing '%s'\n", SNAP_SHOT_DLL);
					  OutputDebugStringW(errorInfo);

					  return HRESULT_FROM_WIN32(error);
				  }
			  }

			  if (m_func == 0) {
				  m_func = (Activate_t) GetProcAddress(m_hDLL, "_LuoSnapShot_Activate@8");
				  if (m_func == 0) {
					  DWORD error = GetLastError();
					  OutputDebugStringW(L"\n ERROR: GetProcAddress - _LuoSnapShot_Activate\n");
					  return HRESULT_FROM_WIN32(error);
				  }
			  }

			  HRESULT hRslt = m_func(token, obj);
			  if (FAILED(hRslt)) {
				  return hRslt;
			  }

			  return S_OK;
		  }
	};

	// DLLibLoader
	HRESULT STDCALL Activate_import(LPCWSTR token, void ** obj)
	{
		static DLLibLoader s_Loader;

		HRESULT hRslt = s_Loader.Activate(token, obj);
		if (FAILED(hRslt)) {
			return hRslt;
		}

		return S_OK;
	}
}	// namespace LuoSnapShot
/* */
